package top.zhangjianyong.tools.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import top.zhangjianyong.tools.entity.marirui.JJLSKX;
import top.zhangjianyong.tools.utils.DateTimeUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author ： zhangjianyong
 * @date ： 2022/9/29 3:44 PM
 */
@Data
@TableName("etf_data")
public class ETFData extends BaseEntity {

    private String code; //代码
    private String name;
    private LocalDate valueDate;//日期
    @TableField(exist = false)
    private LocalDateTime dateTime;
    private BigDecimal value;//单位净值
    private BigDecimal totalValue;//累计净值
    private BigDecimal dailyRate;//日增长率
    private Long  volume;//成交量

    @TableField(exist = false)
    private BigDecimal avgValue;//辅助计算值

    public ETFData() {
    }

    public ETFData(ETF etf) {
        this.code = etf.getCode();
        this.name = etf.getName();
        this.valueDate = etf.getValueDate().toLocalDate();
        this.dateTime = etf.getValueDate();
        this.value = etf.getValue();
        this.totalValue = etf.getTotalValue();
        this.dailyRate = etf.getDailyRate();
    }

    public ETFData(JJLSKX kx, ETF etf) {
        this.code = etf.getCode();
        this.name = etf.getName();
        this.valueDate = DateTimeUtil.parseDate(kx.getD());
        this.value = new BigDecimal(kx.getC());
        this.totalValue = new BigDecimal(kx.getC());
        this.volume= Long.valueOf(kx.getV());
        if (kx.getZd() == null) {
            this.dailyRate = BigDecimal.ONE;
        } else {
            this.dailyRate = new BigDecimal(kx.getZd());
        }

    }

    public BigDecimal getTotalRate() {
        return new BigDecimal(100).add(dailyRate)
                .divide(new BigDecimal(100), 4, RoundingMode.HALF_UP);
    }
}
